﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Runtime.InteropServices.WindowsRuntime;
using System.IO.IsolatedStorage;
using OnlineVideos;
using Common.Library;
using System.Windows.Threading;
using System.Windows.Controls.Primitives;
using Microsoft.Phone.Tasks;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.Foundation;
using Windows.Foundation.Collections;

namespace Social_Celebrities
{    
    public partial class App : Application
    {
        public PhoneApplicationFrame RootFrame { get; private set; }
        public App()
        {
            UnhandledException += Application_UnhandledException;

            InitializeComponent();

            InitializePhoneApplication();

            if (System.Diagnostics.Debugger.IsAttached)
            {
                Application.Current.Host.Settings.EnableFrameRateCounter = true;

                PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled;
            }
        }

        private void Application_Launching(object sender, LaunchingEventArgs e)
        {
            DispatcherTimer Apptimer = new DispatcherTimer();
            Apptimer.Interval = TimeSpan.FromSeconds(61);
            Apptimer.Tick += Apptimer_Tick;
            Apptimer.Start();
            SQLite.SQLiteAsyncConnection conn = new SQLite.SQLiteAsyncConnection(Constants.DataBaseConnectionstringForSqlite);
            Constants.connection = conn;
            var settings = IsolatedStorageSettings.ApplicationSettings;

            if (System.Reflection.Assembly.GetCallingAssembly().FullName.Split('=')[1].Split(',')[0] != SettingsHelper.getStringValue("Version"))
            {
                DataHelper obj = new DataHelper();
                obj.MoveReferenceDatabase();
                obj.CopyDatabase();
                SettingsHelper.AddOrUpdateKey(settings, "Version", System.Reflection.Assembly.GetCallingAssembly().FullName.Split('=')[1].Split(',')[0]);
            }
        }

        void Apptimer_Tick(object sender, EventArgs e)
        {
            (sender as DispatcherTimer).Stop();
            if (AppSettings.AppLaunchCount == AppSettings.ratingreviewpopcount)
            {

                Border frameBorder = (Border)VisualTreeHelper.GetChild(Application.Current.RootVisual, 0);
                Popup Adcforrating = frameBorder.FindName("popforapprating") as Popup;
                Adcforrating.IsOpen = true;
            }
            AppSettings.AppLaunchCount = AppSettings.AppLaunchCount + 1;

        }
        private void Application_Activated(object sender, ActivatedEventArgs e)
        {

        }

        private void Application_Deactivated(object sender, DeactivatedEventArgs e)
        {
        }

        private void Application_Closing(object sender, ClosingEventArgs e)
        {
        }

        private void RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            if (System.Diagnostics.Debugger.IsAttached)
            {
                System.Diagnostics.Debugger.Break();
            }
        }

        private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
        {
            if (System.Diagnostics.Debugger.IsAttached)
            {
                System.Diagnostics.Debugger.Break();
            }
        }

        #region Phone application initialization

        // Avoid double-initialization
        private bool phoneApplicationInitialized = false;

        // Do not add any additional code to this method
        private void InitializePhoneApplication()
        {
            if (phoneApplicationInitialized)
                return;

            // Create the frame but don't set it as RootVisual yet; this allows the splash
            // screen to remain active until the application is ready to render.
            RootFrame = new PhoneApplicationFrame();
            if (App.Current.Resources["AdVisible"] as string == "True")
            {
                RootFrame.Style = App.Current.Resources["RootFrameStyle"] as Style;
                RootFrame.ApplyTemplate();
            }
            RootFrame.Navigated += CompleteInitializePhoneApplication;

            // Handle navigation failures
            RootFrame.NavigationFailed += RootFrame_NavigationFailed;

            // Ensure we don't initialize again
            phoneApplicationInitialized = true;
        }

        // Do not add any additional code to this method
        private void CompleteInitializePhoneApplication(object sender, NavigationEventArgs e)
        {
            // Set the root visual to allow the application to render
            if (RootVisual != RootFrame)
                RootVisual = RootFrame;

            // Remove this handler since it is no longer needed
            RootFrame.Navigated -= CompleteInitializePhoneApplication;
        }

        #endregion

        private void pop_Opened_1(object sender, EventArgs e)
        {
            DispatcherTimer timer1 = new DispatcherTimer();
            timer1.Interval = TimeSpan.FromSeconds(5);
            timer1.Tick += timer1_Tick;
            timer1.Start();
        }

        void timer1_Tick(object sender, EventArgs e)
        {
            (sender as DispatcherTimer).Stop();
            Border frameBorder = (Border)VisualTreeHelper.GetChild(Application.Current.RootVisual, 0);
            Popup Adc = frameBorder.FindName("pop") as Popup;
            Adc.IsOpen = false;
        }

        private void popforapprating_Opened_1(object sender, EventArgs e)
        {
        }

        private void okbtn_Click_1(object sender, RoutedEventArgs e)
        {
            Border frameBorder = (Border)VisualTreeHelper.GetChild(Application.Current.RootVisual, 0);
            Popup Adcrating = frameBorder.FindName("popforapprating") as Popup;
            Adcrating.IsOpen = false;
            AppSettings.ratingreviewpopcount = 50;
            AppSettings.AppLaunchCount = 0;
            MarketplaceReviewTask marketplaceReviewTask = new MarketplaceReviewTask();
            marketplaceReviewTask.Show();

        }

        private void cancelbtn_Click_1(object sender, RoutedEventArgs e)
        {
            Border frameBorder = (Border)VisualTreeHelper.GetChild(Application.Current.RootVisual, 0);
            Popup Adcrating = frameBorder.FindName("popforapprating") as Popup;
            Adcrating.IsOpen = false;
            AppSettings.ratingreviewpopcount = 20;
            AppSettings.AppLaunchCount = 0;
        }
    }
}